package home_8_25;

import java.sql.*;

public class studentDemo {
    public static void main(String[] args) {
        studentDemo stu1 = new studentDemo();

        String sql1 = "update student set ssex = '2b'" +
                " where sno = 102";

        String sql2  = "insert into student (sno,sname,ssex) values(103,'王菲','女')\n ";
        int mm = stu1.executeUpdate(sql2);
        if (mm>0){
            System.out.println("执行成功");
        }else {
            System.out.println("执行失败");
        }


    }
    public int executeUpdate(String sql){
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/jdbc? " +
                "serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false";
        String user = "root";
        String password = "root123";

        Connection conn = null;
        Statement stmt = null;
//        ResultSet rst = null;

        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


        try {
            conn = DriverManager.getConnection(url,user,password);
            stmt = conn.createStatement();
            sql = sql;
            int r = stmt.executeUpdate(sql);

            if (r > 0) {
                System.out.println("成功");

            }
//            rst = stmt.executeQuery(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {

        }
        /*if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                stmt = null;
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                conn = null;
            }
        }*/

        return 0;
    }
}
